package io.sequence_io;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.SequenceInputStream;
import java.util.Enumeration;
import java.util.Vector;

public class IOSequenceInputStream {
    public static void main(String[] args) throws IOException {
        //extracted();
        //extracted1();整合两个输入流
        //extracted2();整合多个流


    }

    private static void extracted2() throws IOException {
        FileInputStream fis1 = new FileInputStream("1.tct");
        FileInputStream fis2 = new FileInputStream("2.txt");
        FileInputStream fis3 = new FileInputStream("3.txt");

        Vector<FileInputStream> v = new Vector<>();
        v.add(fis1);
        v.add(fis2);
        v.add(fis3);

        Enumeration<FileInputStream> en = v.elements();//枚举
        //将枚举中的输入流整合成一个
        SequenceInputStream sis = new SequenceInputStream(en);
        FileOutputStream fos = new FileOutputStream("d.txt");

        int b;
        while((b = sis.read()) != -1) {
            fos.write(b);
        }
        sis.close();
        fos.close();
    }

    private static void extracted1() throws IOException {
        FileInputStream fis1 = new FileInputStream("D:\\wangdao_study\\studyHard\\yyy.txt");
        FileInputStream fis2 = new FileInputStream("D:\\wangdao_study\\studyHard\\添狗.txt");

        SequenceInputStream sis = new SequenceInputStream(fis1,fis2);
        FileOutputStream fos = new FileOutputStream("x.txt");

        int b;
        while((b = sis.read()) != -1) {
            fos.write(b);
        }
        sis.close();//sis在关闭的时候，会将构造方法中传入的流对象也关闭
        fos.close();
    }

    private static void extracted() throws IOException {
        //创建字节输入流，关联"舔狗1.txt"
        FileInputStream fis1 = new FileInputStream("舔狗1.txt");
        //创建字节输入流，关联"舔狗2.txt"
        FileInputStream fis2 = new FileInputStream("舔狗2.txt");

        FileOutputStream fos = new FileOutputStream("Sequence.txt");

        int b1;
        while((b1 = fis1.read()) != -1) {
            fos.write(b1);
        }
        fis1.close();

        int b2;
        while((b2 = fis2.read()) != -1) {
            fos.write(b2);
        }
        fis2.close();
        fos.close();
    }
}
